package org.uugu.service.impl;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.uugu.dao.UsersDAO;
import org.uugu.model.Users;
import org.uugu.service.AllService;
import org.uugu.service.UsersManager;



@Component("usersManager")
public class UsersManagerImpl extends AllService implements UsersManager {

	// 自动注入userDao，也可以使用@Resource
	
	private UsersDAO usersDao;
	private Log log = LogFactory.getLog(UsersManagerImpl.class);
	
//=================================用户添加====================================// 
	@Override
	public String add(Users user) {
		if (usersDao.findByUsername(user.getUsername()).size() == 0) {
			try {
				usersDao.save(user);
				log.debug("add User:" + user.getUsername());
				return "addSuccess";
			} catch (Exception e) {
				return "addFalse";
			}
		} else {
			return "addFalse";
		}
	}

	//=================================用户密码更新====================================// 
	@Override
	public String update(Users user) {
		try {
			usersDao.getHibernateTemplate().bulkUpdate("update Users as u set u.password='"+user.getPassword()+"' where u.username='"+user.getUsername()+"'");
			//usersDao.merge(user);
			log.debug("update User:" + user.getUsername());
			return "updateSuccess";
		} catch (Exception e) {
			return "updateFalse";
		}
	}
	
//=================================用户登录====================================// 
	@Override
	public String login(Users user) {
		try {
			if (usersDao.findByUsername(user.getUsername()).get(0)
					.getPassword().equals(user.getPassword())) {
				// log.info("==============="+usersDao.findByUsername(user.getPassword()));
			
				return "loginsuccess";
			} else {
				log.info(user.getUsername() + "=======" + user.getPassword()
						+ "====不存在！");
				// log.info("==============="+usersDao.findByUsername(user.getUsername()).get(0).getPassword());
				return "loginfalse";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "loginfalse";
		}
	}

//===============================Getters And Setters==========================//
	public UsersDAO getUsersDao() {
		return usersDao;
	}
	@Resource
	public void setUsersDao(UsersDAO usersDao) {
		this.usersDao = usersDao;
	}
}
